import Vue from "vue";
import Vuex from "vuex";
import {
  getUserProfiles,
  getProductRec,
  getProductHot,
} from "../request/httpApi";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userInfo: null,
    productRec: {},
    productHot: {},
  },
  mutations: {
    // 更新用户信息
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
    // 更新精品推荐商品
    updateProductRec(state, product) {
      state.productRec = product;
    },
    // 更新热门兑换商品
    updateProductHot(state, product) {
      state.productHot = product;
    },
  },
  actions: {
    // 用户信息获取
    getUserInfo(context) {
      return new Promise((resolve, reject) => {
        getUserProfiles().then(res => {
          if (res.code === 0) {
            context.commit("updateUserInfo", res.data);
            resolve();
          }
        });
      });
    },
    // 精品推荐获取
    getProductREC(context) {
      return new Promise((resolve, reject) => {
        getProductRec().then(res => {
          if (res.code === 0) {
            context.commit("updateProductRec", res.data.data.records);
            resolve();
          }
        });
      });
    },
    // 热门兑换获取
    getProductHOT(context) {
      return new Promise((resolve, reject) => {
        getProductHot().then(res => {
          if (res.code === 0) {
            context.commit("updateProductHot", res.data.data.records);
            resolve();
          }
        });
      });
    },
  },
  modules: {},
});
